package com.lagou.service.impl;

import com.lagou.annotations.AutoWired;
import com.lagou.annotations.Service;
import com.lagou.annotations.Transactional;
import com.lagou.pojo.Account;
import com.lagou.repository.AccountRepository;
import com.lagou.service.TransferService;

/**
 * 转账业务的实现
 */
@Service("transferService")
@Transactional(methodPrefix = "transfer")
public class TransferServiceImpl implements TransferService {
    @AutoWired
    private AccountRepository accountRepository;

    @Override
    public void transfer(String fromCardId, String toCardId, int money) throws Exception {
        Account fromAccount = accountRepository.selectAccountByCardId(fromCardId);
        Account toAccount = accountRepository.selectAccountByCardId(toCardId);

        fromAccount.setMoney(fromAccount.getMoney() - money);
        toAccount.setMoney(toAccount.getMoney() + money);

        accountRepository.updateAccountByCardId(fromAccount);
        accountRepository.updateAccountByCardId(toAccount);
    }
}
